package cn.edu.qzuie.artist.module.td.controller.admin.work;

import cn.edu.qzuie.artist.module.td.dal.dataobject.worksimage.WorksImageDO;
import cn.edu.qzuie.artist.module.td.dal.mysql.worksimage.WorksImageMapper;
import cn.edu.qzuie.artist.module.td.service.worksimage.WorksImageService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.edu.qzuie.artist.framework.common.pojo.PageParam;
import cn.edu.qzuie.artist.framework.common.pojo.PageResult;
import cn.edu.qzuie.artist.framework.common.pojo.CommonResult;
import cn.edu.qzuie.artist.framework.common.util.object.BeanUtils;

import static cn.edu.qzuie.artist.framework.common.pojo.CommonResult.error;
import static cn.edu.qzuie.artist.framework.common.pojo.CommonResult.success;

import cn.edu.qzuie.artist.framework.excel.core.util.ExcelUtils;

import cn.edu.qzuie.artist.framework.apilog.core.annotation.ApiAccessLog;
import static cn.edu.qzuie.artist.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.edu.qzuie.artist.module.td.controller.admin.work.vo.*;
import cn.edu.qzuie.artist.module.td.dal.dataobject.work.WorkDO;
import cn.edu.qzuie.artist.module.td.service.work.WorkService;

@Tag(name = "管理后台 - 作品")
@RestController
@RequestMapping("/td/work")
@Validated
public class WorkController {

    private static final String CANNOT_DELETE = "此作品下有其他作品，不能删除";

    @Resource
    private WorkService workService;

    @Resource
    private WorksImageService worksImageService;

    @PostMapping("/create")
    @Operation(summary = "创建作品")
    @PreAuthorize("@ss.hasPermission('td:work:create')")
    public CommonResult<Long> createWork(@Valid @RequestBody WorkSaveReqVO createReqVO) {
        return success(workService.createWork(createReqVO));
    }
//    @PostMapping("/creates")
//    @Operation(summary = "创建作品")
//    @PreAuthorize("@ss.hasPermission('td:work:create')")
//    public CommonResult<Long> createWorks(@Valid @RequestBody WorkSaveReqVO createReqVO, @RequestParam Long workId) {
//        return success(workService.createWorks(createReqVO, workId));
//    }
    @PutMapping("/update")
    @Operation(summary = "更新作品")
    @PreAuthorize("@ss.hasPermission('td:work:update')")
    public CommonResult<Boolean> updateWork(@Valid @RequestBody WorkSaveReqVO updateReqVO) {
        workService.updateWork(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除作品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('td:work:delete')")
    public CommonResult<Boolean> deleteWork(@RequestParam("id") Long id) {
        //判断一下子表是否有数据,如果有数据不能删除
        List<WorksImageDO> worksImageDOList = worksImageService.getByWorkIdWithDelete(id);
        if (worksImageDOList.size()>0 || worksImageDOList == null){
//            throw new RuntimeException("作品下有图片，不能删除");
            return error(400, CANNOT_DELETE);
        }else {
            workService.deleteWork(id);
            return success(true);
        }

    }
    //原代码
//    @GetMapping("/get")
//    @Operation(summary = "获得作品")
//    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('td:work:query')")
//    public CommonResult<WorkRespVO> getWork(@RequestParam("id") Long id) {
//        WorkDO work = workService.getWork(id);
//        return success(BeanUtils.toBean(work, WorkRespVO.class));
//    }
    @GetMapping("/get")
    @Operation(summary = "获得作品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('td:work:query')")
    public CommonResult<WorkRespVO> getWork(@RequestParam("id") Long id) {
        WorkRespVO work = workService.getWorks(id);
        return success(work);
    }
    @GetMapping("/get-images")
    @Operation(summary = "获得作品集")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('td:work:query')")
    public CommonResult<List<WorGetImageRespVO>> getImages(@RequestParam("id") Long id) {
        List<WorGetImageRespVO> work = workService.getImagesWithDescribes(id);
        return success(work);
    }
//    @GetMapping("/page")
//    @Operation(summary = "获得作品分页")
//    @PreAuthorize("@ss.hasPermission('td:work:query')")
//    public CommonResult<PageResult<WorkRespVO>> getWorkPage(@Valid WorkPageReqVO pageReqVO) {
//        PageResult<WorkDO> pageResult = workService.getWorkPage(pageReqVO);
//        return success(BeanUtils.toBean(pageResult, WorkRespVO.class));
//    }

    @GetMapping("/page")
    @Operation(summary = "获得作品分页")
    @PreAuthorize("@ss.hasPermission('td:work:query')")
    public CommonResult<PageResult<WorkMultiplePagesVO>> getWorkPage(@Valid WorkPageReqVO pageReqVO) {
        PageResult<WorkMultiplePagesVO> pageResult = workService.getWorkWithcategoryName(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WorkMultiplePagesVO.class));

    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出作品 Excel")
    @PreAuthorize("@ss.hasPermission('td:work:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWorkExcel(@Valid WorkPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorkDO> list = workService.getWorkPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "作品.xls", "数据", WorkRespVO.class,
                        BeanUtils.toBean(list, WorkRespVO.class));
    }

    @PostMapping("/published/{published}")
    @Operation(summary = "更新作品")
    @PreAuthorize("@ss.hasPermission('td:work:update')")
    public CommonResult<Boolean> updateWorkPublished(Long id,@PathVariable("published") Boolean published) {
        //更新操作，如果published为true，则设置为已发布，为false则设置为未发布
        workService.updateWorkPublished(published, id);
        return success(true);
    }


}